﻿using System;
using System.Data.Objects;
using System.Linq;

namespace TestFileSystem.Models.Directory
{
    /// <summary>
    /// Репозиторий директорий
    /// </summary>
    public class DirectoryRepository : IRepository<DBDirectory>, IDisposable
    {
        private readonly TestFileSystemEntities _dataContext;

        public DirectoryRepository()
        {
            _dataContext = new TestFileSystemEntities();
        }

        public DirectoryRepository(TestFileSystemEntities dataContext)
        {
            _dataContext = dataContext;
        }

        /// <summary>
        /// Возвращает список директорий
        /// </summary>
        /// <returns>Спсиок директорий</returns>
        public ObjectSet<DBDirectory> GetAllObjects()
        {
            return _dataContext.DBDirectories;
        }

        public void InsertObject(DBDirectory newObject)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Возвращает директорию по идентификатору
        /// </summary>
        /// <param name="id">Идентификатор директории</param>
        /// <returns>Выбираемая директория</returns>
        public DBDirectory GetObjectById(int id)
        {
            return _dataContext.DBDirectories.SingleOrDefault(d => d.Id == id);
        }

        public void UpdateObject(DBDirectory updatedObject)
        {
            throw new NotImplementedException();
        }

        private bool _disposed = false;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if(!_disposed)
            {
                if (disposing)
                {
                    _dataContext.Dispose();
                }
            }

            _disposed = true;
        }

        ~DirectoryRepository()
        {
            Dispose(false);
        }
    }
}